<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>
<%@ include file="/common/piny.jsp"%>
<%
    String key = input.getDesc("key", "-1");
    String branch = input.getDesc("branch", "");
    Integer type = input.getInt("type", DEPT_ALL);  // 不传默认科室介绍DEPT_ALL
    Where where = new Where();
    if (!"".equals(branch)) {
        where.and("branch=", branch);
    }
    if (DEPT_REGISTER.equals(type)) {
        where.and("type in("+DEPT_ORDINARY+","+DEPT_EXPERT+")");
    } else {
        where.and("type=", type);
    }
    List<Map> list = QBuilder.db("basedb").select("new map(id as id, deptId as dept_id, deptName as dept_name, parent as parent, branch as branch, branchName as branch_name, type as type)").from("Dept").where(where.and("hospital=", hospital).and("status = ", STATUS_ENABLE)).query().getResultObj();
    if (isEmpty(list)) {
        Tools.encryptPrint(out, new Message(30091, "没搜索到任何相关数据"));
        return;
    }
    // 去除父类
    List<String> ids = new ArrayList<String>();
    List<String> parents = new ArrayList<String>();
    Iterator<Map> iterator = list.iterator();
    while (iterator.hasNext()) {
        Map map = iterator.next();
        ids.add(Tools.getDesc(map.get("id")));
        parents.add(Tools.getDesc(map.get("parent")));
    }
    ids.removeAll(parents);
    List<Map> dest = new ArrayList<Map>();
    for (int i = 0; i < ids.size(); i++) {
        String id = ids.get(i);
        for (int j = 0; j < list.size(); j++) {
            Map map = list.get(j);
            String _id = Tools.getDesc(map.get("id"), "-------");
            if (_id.equals(id)) {
                dest.add(map);
                break;
            }
        }
    }
    if (isEmpty(dest)) {
        Tools.encryptPrint(out, new Message(30091, "没搜索到任何相关数据"));
        return;
    }
    Map<String, Object> temp = new HashMap<String, Object>();
    for (int i = 0; i < dest.size(); i++) {
        Map map = dest.get(i);
        String deptName = Tools.getDesc(map.get("dept_name"), "");
        String qpyDeptName = getPingYin(deptName);
        String hpyDeptName = getPinYinHeadChar(deptName);
        if (deptName.contains(key) || qpyDeptName.contains(key) || hpyDeptName.contains(key)) {
            String branchName = Tools.getDesc(map.get("branch_name"), "本部");
            List<Map> tempList = (List<Map>)temp.get(branchName);
            if (tempList == null) {
                tempList = new ArrayList();
                temp.put(branchName, tempList);
            }
            tempList.add(map);
        }
    }
    Set<Entry<String, Object>> set = temp.entrySet();
    List result = new ArrayList();
    for (Entry<String, Object> entry : set) {
        String _key = entry.getKey();
        List<Map> tempList = (List<Map>)entry.getValue();
        Map tempMap = new HashMap();
        tempMap.put("name", _key);
        tempMap.put("result", tempList);
        result.add(tempMap);
    }
    if (isEmpty(result)) {
        msg = new Message(30091, "没搜索到任何相关数据");
    }
    msg.put("data", result);
    Tools.encryptPrint(out, msg);
%>
